package com.alcargo.tools.sample.main;

import java.util.List;

import org.apache.log4j.Logger;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.cfg.AnnotationConfiguration;

import com.alcargo.tools.mock.EntityBeanAnnotation4Demo;
import com.alcargo.tools.mock.EntityBeanXml4Demo;

public class HibernateSampleMain {
	static Logger logger = Logger.getLogger("HibernateSampleMain");
	public static void main(String[] args) {

		logger.debug("hibernate sample begin");
		HibernateSampleMain me = new HibernateSampleMain();
		
		Session session  = me.makeConfiguration()
								.addClass(EntityBeanXml4Demo.class)
								.addAnnotatedClass(EntityBeanAnnotation4Demo.class)
								.buildSessionFactory()
								.getCurrentSession();
		Transaction t = session.beginTransaction();
		Object o ;
		o= new EntityBeanXml4Demo("maodajun");
		session.save(o);
		o= new EntityBeanXml4Demo("maodajun");
		session.save(o);
		o= new EntityBeanXml4Demo("maodajun");
		session.save(o);
		o= new EntityBeanXml4Demo("maodajun");
		session.save(o);
		o= new EntityBeanXml4Demo("maodajun");
		session.save(o);
		o= new EntityBeanAnnotation4Demo("maodajun","20");
		session.save(o);

		Criteria c = session.createCriteria(EntityBeanXml4Demo.class);
		c.setFirstResult(1);
		c.setMaxResults(3);
		List list =c.list();
//		
//		System.out.println(list.toString());
//		 c = session.createCriteria(EntityBeanAnnotation4Demo.class);
//		 list =c.list();
		System.out.println(list.toString());
		t.commit();

//		AnnotationConfiguration config = me.makeConfiguration();
//		config.addAnnotatedClass(EntityBeanAnnotation4Demo.class);
//		session = config.buildSessionFactory().getCurrentSession();
//		t = session.beginTransaction();
//		o = new EntityBeanAnnotation4Demo("maodajun","20");
//		session.save(o);
//		c = session.createCriteria(EntityBeanAnnotation4Demo.class);
//		list = c.list();
//		System.out.println(list.toString());
//		t.commit();
		
		
		logger.debug("hibernate sample end");

		

		

	}

	public AnnotationConfiguration makeConfiguration() {
		AnnotationConfiguration config = new AnnotationConfiguration();
		config.setProperty("hibernate.connection.driver_class","org.hsqldb.jdbcDriver");
		config.setProperty("hibernate.connection.url","jdbc:hsqldb:mem:dbName");
		config.setProperty("hibernate.connection.username","sa");
		config.setProperty("hibernate.connection.password","");
		config.setProperty("hibernate.connection.pool_size","1");
		config.setProperty("hibernate.dialect","org.hibernate.dialect.HSQLDialect");
		config.setProperty("hibernate.current_session_context_class","thread");
		config.setProperty("hibernate.cache.provider_class","org.hibernate.cache.NoCacheProvider");
		config.setProperty("hibernate.show_sql","true");
		config.setProperty("hibernate.hbm2ddl.auto","create");
		return config;		
	}
	public AnnotationConfiguration makeConfiguration(String str){
		AnnotationConfiguration config = new AnnotationConfiguration();
		if(str.equalsIgnoreCase("oracle")){
			config.setProperty("hibernate.connection.driver_class"		,"oracle.jdbc.OracleDriver"									);
			config.setProperty("hibernate.connection.url"         		,"jdbc:oracle:thin:@192.168.1.117:1521:orcl"				);
			config.setProperty("hibernate.connection.username"    		,"neutral"														);
			config.setProperty("hibernate.connection.password"			,"neutral"															);
			config.setProperty("hibernate.connection.pool_size"			,"1"														);
			config.setProperty("hibernate.dialect"						,"org.hibernate.dialect.Oracle9Dialect"						);
			config.setProperty("hibernate.current_session_context_class","thread"													);
			config.setProperty("hibernate.cache.provider_class"			,"org.hibernate.cache.NoCacheProvider"						);
			config.setProperty("hibernate.show_sql"						,"true"														);
			config.setProperty("hibernate.hbm2ddl.auto"					,"create"													);

		}else if(str.equalsIgnoreCase("mysql")){
			config.setProperty("hibernate.connection.driver_class"		,"org.hsqldb.jdbcDriver"				);
			config.setProperty("hibernate.connection.url"         		,"jdbc:hsqldb:mem:dbName"				);
			config.setProperty("hibernate.connection.username"    		,"sa"									);
			config.setProperty("hibernate.connection.password"			,""										);
			config.setProperty("hibernate.connection.pool_size"			,"1"									);
			config.setProperty("hibernate.dialect"						,"org.hibernate.dialect.HSQLDialect"	);
			config.setProperty("hibernate.current_session_context_class","thread"								);
			config.setProperty("hibernate.cache.provider_class"			,"org.hibernate.cache.NoCacheProvider"	);
			config.setProperty("hibernate.show_sql"						,"true"									);
			config.setProperty("hibernate.hbm2ddl.auto"					,"create"								);

		}else{
			config.setProperty("hibernate.connection.driver_class"		,"org.hsqldb.jdbcDriver"				);
			config.setProperty("hibernate.connection.url"         		,"jdbc:hsqldb:mem:dbName"				);
			config.setProperty("hibernate.connection.username"    		,"sa"									);
			config.setProperty("hibernate.connection.password"			,""										);
			config.setProperty("hibernate.connection.pool_size"			,"1"									);
			config.setProperty("hibernate.dialect"						,"org.hibernate.dialect.HSQLDialect"	);
			config.setProperty("hibernate.current_session_context_class","thread"								);
			config.setProperty("hibernate.cache.provider_class"			,"org.hibernate.cache.NoCacheProvider"	);
			config.setProperty("hibernate.show_sql"						,"true"									);
			config.setProperty("hibernate.hbm2ddl.auto"					,"create"								);

		}
		return config;
	}

}
